//
//  NSDate+GRJDate.m
//  GRJTolCayFrwk
//
//  Created by apple on 2017/3/1.
//  Copyright © 2017年 Global Barter. All rights reserved.
//

#import "NSDate+GRJDate.h"

@implementation NSDate (GRJDate)

- (BOOL)isThisYear
{
    // 判断是否是今年
    // 获取日历对象
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    // 获取发布日期组件
    NSDateComponents *cmp = [currentCalendar components:NSCalendarUnitYear fromDate:self];
    // 获取当前日期组件
    NSDateComponents *cmpCurrent = [currentCalendar components:NSCalendarUnitYear fromDate:[NSDate date]];
    
    return cmp.year == cmpCurrent.year;
}
- (BOOL)isThisToday
{
    // 判断是否是今天
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    
    return [currentCalendar isDateInToday:self];
}

- (BOOL)isThisYesterday
{
    // 判断是否是昨天
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    
    return [currentCalendar isDateInYesterday:self];
}

- (NSDateComponents *)detalDateWithNow
{
    // 判断下发布日期 与 当前日期 小时，分差值
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSInteger unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    // 获取两个日期的差值，获取发布日期与当前日期差值
    return [currentCalendar components:unit  fromDate:self toDate:[NSDate date] options:NSCalendarWrapComponents];
}








/**
 *  是否为今天
 */
- (BOOL)isToday
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    return
    (selfCmps.year == nowCmps.year) &&
    (selfCmps.month == nowCmps.month) &&
    (selfCmps.day == nowCmps.day);
}

/**
 *  是否为昨天
 */
- (BOOL)isYesterday
{
    // 2014-05-01
    NSDate *nowDate = [[NSDate date] dateWithYMD];
    
    // 2014-04-30
    NSDate *selfDate = [self dateWithYMD];
    
    // 获得nowDate和selfDate的差距
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    return cmps.day == 1;
}

- (NSDate *)dateWithYMD
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *selfStr = [fmt stringFromDate:self];
    return [fmt dateFromString:selfStr];
}

///**
// *  是否为今年
// */
//- (BOOL)isThisYear
//{
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    int unit = NSCalendarUnitYear;
//    
//    // 1.获得当前时间的年月日
//    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
//    
//    // 2.获得self的年月日
//    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
//    
//    return nowCmps.year == selfCmps.year;
//}

- (NSDateComponents *)deltaWithNow
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
}


@end
